package com.rslai.base.tool.servertest.model;

import java.util.*;

import org.apache.commons.lang.StringUtils;

public class TestSuite extends MappedElement implements Comparable<TestSuite> {
    private String id;
    private String desc;
    private List<String> tags;
    private String caseFileName;
    private List<TestCase> testCases;
    private List<TestCase> backGrounds;
    private List<TestCase> beforeSuite;
    private List<TestCase> afterSuite;
    private List<TestCase> beforeCase;
    private List<TestCase> afterCase;

    public String getId() {
        if (StringUtils.isBlank(this.id)) return this.desc;
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<TestCase> getTestCases() {
        return this.testCases;
    }

    public void setTestCases(List<TestCase> testCases) {
        this.testCases = testCases;
    }

    public String getCaseFileName() {
        return this.caseFileName;
    }

    public void setCaseFileName(String caseFileName) {
        this.caseFileName = caseFileName;
    }

    public List<TestCase> getBackGrounds() {
        if (this.backGrounds == null) {
            this.backGrounds = new ArrayList();
        }
        return this.backGrounds;
    }

    public void setBackGrounds(List<TestCase> backGrounds) {
        this.backGrounds = backGrounds;
    }

    public String getDesc() {
        if (StringUtils.isBlank(this.desc)) return this.id;
        return this.desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public List<String> getTags() {
        return this.tags;
    }

    public void setTag(List<String> tags) {
        this.tags = tags;
    }

    public List<TestCase> getBeforeSuite() {
        return this.beforeSuite;
    }

    public void setBeforeSuite(List<TestCase> beforeSuite) {
        this.beforeSuite = beforeSuite;
    }

    public List<TestCase> getAfterSuite() {
        return this.afterSuite;
    }

    public void setAfterSuite(List<TestCase> afterSuite) {
        this.afterSuite = afterSuite;
    }

    public List<TestCase> getBeforeCase() {
        return this.beforeCase;
    }

    public void setBeforeCase(List<TestCase> beforeCase) {
        this.beforeCase = beforeCase;
    }

    public List<TestCase> getAfterCase() {
        return this.afterCase;
    }

    public void setAfterCase(List<TestCase> afterCase) {
        this.afterCase = afterCase;
    }

    public Map asMap() {
        Map suiteMap = new HashMap();
        suiteMap.put("id", getId());
        suiteMap.put("name", getDesc());
        suiteMap.put("keyword", "Feature");
        suiteMap.put("uri", getCaseFileName());
        suiteMap.put("comments", "");
        suiteMap.put("tags", getTags(this.tags));
        return suiteMap;
    }

    public TestSuite clone() {
        TestSuite testSuite = new TestSuite();
        testSuite.setDesc(this.desc);
        testSuite.setId(this.id);
        testSuite.setCaseFileName(this.caseFileName);
        testSuite.setTag(cloneTag(this.tags));
        testSuite.setTestCases(cloneCommand(this.testCases));
        testSuite.setAfterCase(cloneCommand(this.afterCase));
        testSuite.setAfterSuite(cloneCommand(this.afterSuite));
        testSuite.setBackGrounds(cloneCommand(this.backGrounds));
        testSuite.setBeforeCase(cloneCommand(this.beforeCase));
        testSuite.setBeforeSuite(cloneCommand(this.beforeSuite));

        return testSuite;
    }

    private List<TestCase> cloneCommand(List<TestCase> testCaseList) {
        if (testCaseList != null) {
            List testCases = new ArrayList();
            for (TestCase testCase : testCaseList) {
                if ((testCase instanceof DataDrivenTestCase))
                    testCases.add(((DataDrivenTestCase) testCase).clone((DataDrivenTestCase) testCase));
                else {
                    testCases.add(testCase.clone());
                }
            }
            return testCases;
        }
        return Collections.EMPTY_LIST;
    }

    private List<String> cloneTag(List<String> tagList) {
        if (this.tags != null) {
            List tags = new ArrayList();
            String tag;
            for (Iterator i$ = tags.iterator(); i$.hasNext(); tags.add(tag))
                tag = (String) i$.next();

            return tags;
        } else {
            return Collections.EMPTY_LIST;
        }
    }

    public int compareTo(TestSuite testSuite) {
        return getDesc().compareTo(testSuite.getDesc());
    }
}
